/**
 * Copyright 2013 DJSearch Co.,Ltd. All rights reserved.
 * DTM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.djsearch.controller;

import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpResponse;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.json.JSONException;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * @author dingzhongchang
 * @version 1.0.0
 */
public class DefaultController extends Controller {


    @Override
    public void post(IRequest request, final IAsyncHandler handler) {
        RequestParams params = new RequestParams();
        String str = request.toString();
        if (!"null".equals(str) && !"{}".equals(str)) {
            params.put("data", request.toParams());
            Log.d("HTTP", "POST==>" + request.url() + "?" + params.toString());
        } else {
            Log.d("HTTP", "POST==>" + request.url());
        }
        client.post(request.url(), params, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                if (handler != null) {
                    HttpProtocolParams.setUseExpectContinue(client
                            .getHttpClient().getParams(), false);
                    HttpProtocolParams.setUserAgent(client.getHttpClient()
                            .getParams(), "android-async-http");
                    handler.onStart();
                }
            }

            @Override
            public void onFailure(Throwable error, String content) {
                if (handler != null) {
                    handler.onFailure(error, content);
                }
            }

            @Override
            public void onFinish() {
                if (handler != null) {
                    handler.onFinish();
                }
            }

            @Override
            public void onSuccess(String content) {
                if (handler != null) {
                    try {
                        handler.onSuccess(new JSONResp(content));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    handler.onSuccess(content);
                }
            }

        });
    }

    public static DefaultController getHttpClient() {
        return DefaultControllerHolder.Controller;
    }

    static class DefaultControllerHolder {
        public static DefaultController Controller = new DefaultController();
    }

    private DefaultController() {
        super();
    }
}
